package com.aqie.meet.ui.my;

import android.content.Intent;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.aqie.libbase.event.EventManager;
import com.aqie.libbase.event.MessageEvent;
import com.aqie.libcommon.app.BaseApplication;
import com.aqie.libcommon.app.ToolbarActivity;
import com.aqie.libfactory.model.api.RspModel;
import com.aqie.libfactory.net.Network;
import com.aqie.libfactory.net.RemoteService;
import com.aqie.meet.R;
import com.aqie.meet.model.FriendPermission;
import com.aqie.meet.ui.login.AccountActivity;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 朋友权限
 */
public class FriendFindPermissionActivity extends ToolbarActivity {

    @BindView(R.id.s_v1)
    Switch mSWitch1;

    @BindView(R.id.s_v2)
    Switch mSWitch2;

    private String talentsId;

    // true 不看 1 不看
    private Boolean unLook = false;
    private Boolean unSetLook = false;
    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_friend_find_permission;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        // 页面传参用人id
        Intent intent = getIntent();
        talentsId = intent.getStringExtra("talentsId");
        unLook = intent.getIntExtra("unLook", -1) == 1;
        unSetLook = intent.getIntExtra("unGetLook", -1) == 1;
        Log.e(TAG, "- " + talentsId + " " + unLook + " " + unSetLook);
        setTitle(R.string.friend_find_permission);
        if(unSetLook) {
            mSWitch1.setChecked(true);
            mSWitch1.setSwitchTextAppearance(getApplication(),R.style.s_true);
        } else {
            mSWitch1.setChecked(false);
            mSWitch1.setSwitchTextAppearance(getApplication(),R.style.s_false);
        }
        if(unLook) {
            mSWitch2.setChecked(true);
            mSWitch2.setSwitchTextAppearance(getApplication(),R.style.s_true);
        } else {
            mSWitch2.setChecked(false);
            mSWitch2.setSwitchTextAppearance(getApplication(),R.style.s_false);
        }

        mSWitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                MessageEvent<FriendPermission> event = new MessageEvent<>(EventManager.EVENT_FRIEND_PERMISSION);
                FriendPermission permission = new FriendPermission();
                if (b) {
                    Log.e(TAG, "open1");
                    unSetLook = true;
                    mSWitch1.setSwitchTextAppearance(getApplication(),R.style.s_true);
                }else {
                    Log.e(TAG, "close1");
                    unSetLook = false;
                    mSWitch1.setSwitchTextAppearance(getApplication(),R.style.s_false);
                }
                permission.setUnLook(unLook ? 1 : 0);
                permission.setUnGetLook(unSetLook ? 1 : 0);
                event.setData(permission);
                EventManager.post(event);
                friendPermission(talentsId,unLook,unSetLook);
            }
        });


        mSWitch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                MessageEvent<FriendPermission> event = new MessageEvent<>(EventManager.EVENT_FRIEND_PERMISSION);
                FriendPermission permission = new FriendPermission();
                if (b) {
                    Log.e(TAG, "open2");
                    unLook = true;
                    mSWitch2.setSwitchTextAppearance(getApplication(),R.style.s_true);
                }else {
                    Log.e(TAG, "close2");
                    unLook = false;
                    mSWitch2.setSwitchTextAppearance(getApplication(),R.style.s_false);
                }
                permission.setUnLook(unLook ? 1 : 0);
                permission.setUnGetLook(unSetLook ? 1 : 0);
                event.setData(permission);
                EventManager.post(event);
                friendPermission(talentsId,unLook,unSetLook);
            }
        });
    }

    /**
     * 修改朋友圈权限
     */
    private void friendPermission(String talentsId, Boolean unLook, Boolean unSetLook){
        RemoteService service = Network.remote();
        Call<RspModel<Boolean>> call = service.friendPermission(talentsId, unLook, unSetLook);
        call.enqueue(new Callback<RspModel<Boolean>>() {
            @Override
            public void onResponse(Call<RspModel<Boolean>> call, Response<RspModel<Boolean>> response) {

                RspModel<Boolean> body = response.body();
                if(response.code() == 401){
                    Log.e(TAG, "尚未登录");
                    AccountActivity.show(getApplication());
                    return;
                }
                if(body !=null && body.success()){
                    BaseApplication.showToast("操作成功");
                }else {
                    Log.e(TAG, "request fail");
                }
            }

            @Override
            public void onFailure(Call<RspModel<Boolean>> call, Throwable t) {
                Log.e(TAG, t.getMessage());
            }
        });
    }

    @Override
    protected void initTitleNeedBack() {
        super.initTitleNeedBack();
    }
}